﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WorldBankBBS.Comms;
using WorldBankBBS.Data;
using WorldBankBBS.Translators;
using Session = WorldBankBBS.Logic.Session;

namespace WorldBankBBS.Handlers
{
    public class TextFileHandler
    {
        private readonly ITranslator _translator = null;

        public TextFileHandler(Logic.Session session)
        {
            switch (session.Encoding)
            {
                case "PETSCII":
                    _translator = new CbmTranslator() {CurrentSession = session};
                    break;

                case "ANSI":
                    _translator = new AnsiTranslator() {CurrentSession = session};
                    break;

                default:
                    _translator = new AsciiTranslator() {CurrentSession = session};
                    break;
            }
        }

        public static void OnPreLogin(object sender, Logic.Session session)
        {
            var handler = new TextFileHandler(session);
            handler.SendFile(session, "TextFiles\\prelogin");
        }

        public static void OnPostLogin(object sender, Logic.Session session)
        {
            var handler = new TextFileHandler(session);
            handler.SendFile(session, "TextFiles\\postlogin");
        }

        public void SendFile(Logic.Session session, string filename)
        {
            var buffer = new byte[65535];
            var length = 0;

            var assy = Assembly.GetEntryAssembly().Location;
            var path = Path.GetDirectoryName(assy);
            filename = Path.Combine(path, filename);
            
            if (session.Encoding == "PETSCII")
            {
                if (System.IO.File.Exists(filename + ".petscii"))
                {
                    var text = System.IO.File.ReadAllText(filename + ".petscii");
                    length = _translator.StringToBytes(buffer, text);
                    var tbuffer = buffer.ToArray().WrapText(0, length, session.Encoding, (int)session["ScreenWidth"]);
                    length = tbuffer.Count();

                    for (int i = 0; i < Math.Min(length, buffer.Length); ++i)
                    {
                        buffer[i] = tbuffer[i];
                    }
                }
                else if (System.IO.File.Exists(filename + ".txt"))
                {
                    var text = System.IO.File.ReadAllText(filename + ".txt");
                    length = _translator.StringToBytes(buffer, text);

                    var tbuffer = buffer.ToArray().WrapText(0, length, session.Encoding, (int)session["ScreenWidth"]);
                    length = tbuffer.Count();
                    
                    for (int i = 0; i < Math.Min(length, buffer.Length); ++i)
                    {
                        buffer[i] = tbuffer[i];
                    }
                }

            }
            else if (session.Encoding == "ANSI")
            {
                if (System.IO.File.Exists(filename + ".ans"))
                {
                    var text = System.IO.File.ReadAllText(filename + ".ans");
                    length = _translator.StringToBytes(buffer, text);
                }
                else if (System.IO.File.Exists(filename + ".txt"))
                {
                    var text = System.IO.File.ReadAllText(filename + ".txt");
                    length = _translator.StringToBytes(buffer, text);

                    var tbuffer = buffer.ToArray().WrapText(0, length, session.Encoding, (int)session["ScreenWidth"]);
                    length = tbuffer.Count();

                    for (int i = 0; i < Math.Min(length, buffer.Length); ++i)
                    {
                        buffer[i] = tbuffer[i];
                    }
                }

            }
            else if (System.IO.File.Exists(filename + ".txt"))
            {
                var text = System.IO.File.ReadAllText(filename + ".txt");
                length = _translator.StringToBytes(buffer, text);

                var tbuffer = buffer.ToArray().WrapText(0, length, session.Encoding, (int)session["ScreenWidth"]);
                length = tbuffer.Count();

                for (int i = 0; i < Math.Min(length, buffer.Length); ++i)
                {
                    buffer[i] = tbuffer[i];
                }
            }

            if (length > 0)
            {
                var pages = buffer.Paginate(0, length, 23, session.Encoding);

                for(int p = 0; p < pages.Count; ++p)
                {
                    var page = pages[p];
                    session.Connection.SendData(page);
                    if(p != pages.Count - 1)
                        session.InvokePause(session.Encoding);
                }
            }
        }

        public static void OnLoginFailed(object sender, Session e)
        {
            var handler = new TextFileHandler(e);
            handler.SendFile(e, "TextFiles\\loginfailed");
        }

        public static void OnPreGuestLogin(object sender, Session e)
        {
            var handler = new TextFileHandler(e);
            handler.SendFile(e, "TextFiles\\preguestlogin");
        }

        public static void OnPostGuestLogin(object sender, Session e)
        {
            var handler = new TextFileHandler(e);
            handler.SendFile(e, "TextFiles\\postguestlogin");
        }
    }
}
